#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
Author:Lijiacai
Email:1050518702@qq.com
===========================================
CopyRight@JackLee.com
===========================================
"""
import datetime
import logging
import os
import sys
import json
import requests
import cx_Oracle
import shortuuid
from hashids import Hashids
from elasticsearch import Elasticsearch
from elasticsearch import helpers

cur_dir = os.path.split(os.path.realpath(__file__))[0]
sys.path.append("%s/.." % cur_dir)
import config

sys.path.append("%s/.." % cur_dir)
from res.lijiacai.utils import tool


class OrderDataSet(tool.Oracle2Es):
    oracle_conf = config.oracle_conf
    es_cluster = config.es_cluster

    def _id_func(self, data):
        # return self.get_md5(str(time.time()) + str(random.randint(1, 1000)))
        return data.get("ORDER_NO")

    def update(self, sql: str, sql_fields: list, index: str, es_fields=None, _id_func=None):
        self.client.execute(sql)
        # data = self.client.fetch(query="fetchall()")
        if _id_func == None:
            _id_func = self._id_func
        es_fields = {} if es_fields == None else es_fields
        out = []
        while True:
            i = self.client.fetch(query="fetchone()")
            if not i:
                break
            one = {}
            for j in range(len(i)):
                one[sql_fields[j]] = i[j]

            if es_fields:
                es_data = {'_op_type': 'index',
                           '_index': index,
                           '_type': 'doc',
                           "_id": _id_func(one),
                           '_source': self.json_data(one)}
            else:
                es_data = {'_op_type': 'index',
                           '_index': index,
                           '_type': 'doc',
                           "_id": _id_func(one),
                           '_source': self.json_data(one)}

            out.append(es_data)
            if len(out) == 10000:
                self.es_client.bulk(actions=out)
                out = []

        if out:
            self.es_client.bulk(actions=out)

    def getYesterday(self):
        today = datetime.date.today()
        oneday = datetime.timedelta(days=1)
        yesterday = today - oneday
        return yesterday

    def go(self):
        order_sql = {
            "sql":
                """
                SELECT
                    BIZ_ORDER_ITEM.REAL_GET_TIME,
                    BIZ_ORDER_ITEM.REAL_RET_TIME,
                    BIZ_ORDER_ITEM.REAL_RET_STATION_ID,
                    BIZ_ORDER_ITEM.GET_STATION_ID,
                    BIZ_ORDER_ITEM.ORDER_NO
                FROM
                    PANDA.BIZ_ORDER_ITEM BIZ_ORDER_ITEM 
                WHERE
                    TO_CHAR( BIZ_ORDER_ITEM.REAL_RET_TIME, 'yyyy-mm-dd' ) >= '{}'
                    AND TO_CHAR( BIZ_ORDER_ITEM.REAL_RET_TIME, 'yyyy-mm-dd' ) <= '{}'
                """,
            "fields":
                [
                    "REAL_GET_TIME",
                    "REAL_RET_TIME",
                    "REAL_RET_STATION_ID",
                    "GET_STATION_ID",
                    "ORDER_NO"
                ],
        }
        start_time = self.getYesterday()
        end_time = start_time
        # start_time = "2020-04-01"
        # end_time = "2020-06-01"
        sql = order_sql.get("sql").format(start_time, end_time)
        fields = order_sql.get("fields")
        for i in range(10):
            try:
                self.update(sql=sql, sql_fields=fields, index=config.order_predict_dataset)
                break
            except Exception as e:
                logging.exception(str(e))
        self.close()


if __name__ == '__main__':
    OrderDataSet().go()
